import React, { FormEvent } from 'react'
import request from 'utils/request'

export const Login = () => {
  // 登录请求
  const login = (param: { userName: string; passWord: string }) => {
    ;(async () => {
      let res = await request({
        method: 'POST',
        url: '/login',
        headers: {
          'Content-Type': 'application/json'
        },
        data: JSON.stringify(param)
      })
    })()
  }

  // 登录点击回调
  const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
    event.preventDefault()
    const userName = (event.currentTarget.elements[0] as HTMLInputElement).value
    const passWord = (event.currentTarget.elements[1] as HTMLInputElement).value

    login({ userName, passWord })
  }

  return (
    <>
      <form onSubmit={handleSubmit}>
        <div>
          <label htmlFor="userName">用户名</label>
          <input type="text" id="userName" />
        </div>
        <div>
          <label htmlFor="passWord">密码</label>
          <input type="text" id="passWord" />
        </div>
        <button type="submit">登录</button>
      </form>
    </>
  )
}
